import Vue from 'vue'
export default {
  bind (el, binding, vnode) {
    let {input, blur, query} = binding.value
    let flag = false
    Object.assign(binding.value, {'vnode': vnode})
    // 校验输入
    if (input) {
      el.addEventListener('keyup', () => {
        if (flag) {
          return
        }
        Vue.prototype.$input(binding.value)
      })
      el.addEventListener('compositionstart', () => {
        flag = true
      })
      el.addEventListener('compositionend', () => {
        flag = false
      })
      el.addEventListener('blur', () => {
        switch (binding.value.type) {
          case 'money':
            break
        }
      })
    }
    if (blur) {
      el.addEventListener('compositionstart', () => {
        flag = true
      })
      el.addEventListener('compositionend', () => {
        flag = false
      })
      el.addEventListener('blur', () => {
        if (flag) {
          return
        }
        Vue.prototype.$input(binding.value)
      })
    }
    if (query) {
      // 校验请求
      Vue.prototype._vaildationMap.set(query, binding.value)
      el.addEventListener('click', () => {
        let item = Vue.prototype._vaildationMap.get(query)
        if (item && item.dom) {
          document.body.removeChild(item.dom)
          delete item.dom
        }
      })
    }
  },
  update (el, binding, vnode) {
    let {query} = binding.value
    let item = Vue.prototype._vaildationMap.get(query)
    if (!item) {
      Object.assign(binding.value, {'vnode': vnode})
      Vue.prototype._vaildationMap.set(query, binding.value)
    }
  },
  unbind (el, binding, vnode) {
    let {query} = binding.value
    let items = [...Vue.prototype._vaildationMap.values()]
    items.forEach(item => {
      if (item.dom) {
        document.body.removeChild(item.dom)
      }
    })
    Vue.prototype._vaildationMap.delete(query)
  }
}
